import requests
from api.api_login import ApiLogin

"""
员工管理接口（新增、修改、查询、删除）
"""


class Api(object):
    # 添加员工
    @classmethod
    def add_emp(cls, token_data, emp_data,):
        try:
            resp = requests.post("https://heimahr.itheima.net/api/sys/user", headers=token_data, json=emp_data)
            return resp
        except Exception as e:
            return "添加员工请求失败", e

    # 删除员工
    @classmethod
    def delete_emp(cls, emp_id, token_data):
        try:
            resp = requests.delete(f"https://heimahr.itheima.net/api/sys/user/{emp_id}", headers=token_data)
            return resp
        except Exception as e:
            return "删除员工请求失败", e

    # 查询员工
    @classmethod
    def select_emp(cls, emp_name, token_data):
        try:
            resp = requests.get(f"https://heimahr.itheima.net/api/sys/user?keyword={emp_name}", headers=token_data)
            return resp
        except Exception as e:
            return "查询员工请求失败", e

    # 修改员工
    @classmethod
    def put_emp(cls, emp_id, token_data, put_emp_data):
        try:
            resp = requests.put(f"https://heimahr.itheima.net/api/sys/user/{emp_id}", headers=token_data,
                                json=put_emp_data)
            return resp
        except Exception as e:
            return "修改员工请求失败", e


if __name__ == '__main__':
    # 测试员工管理业务流程
    login_test = {"mobile": "13800000002", "password": "hm#qd@23!"}
    resp_login = ApiLogin.login(login_test)
    login_token = str(resp_login.json().get("data"))
    token_data = {"Authorization": f'Bearer {login_token}'}
    emp_data = {"username": "李四", "mobile": "13800138008",
                "formOfEmployment": 1, "departmentId": 6, "timeOfEntry": "2024-06-01"
        , "abc": 1}

    # data = "correctionTime": "2024-11-23T16:00:00.000Z", "staffPhoto": "","workNumber": ""
    resp_add = Api.add_emp(token_data, emp_data)
    print("添加返回的结果：", resp_add.json())

    resp_get = Api.select_emp('李四', token_data)
    print("查询到的员工信息：", resp_get.json())

    put_emp_data = {"id": (resp_add.json().get("data").get("id")), "mobile": "13800138008", "staffPhoto": "",
                    "username": "饕餮",
                    "workNumber": "HEIMA00121", "timeOfEntry": "2024-06-01", "correctionTime": "2024-11-24",
                    "departmentId": 6, "departmentName": "技术部", "formOfEmployment": 1, "roleIds": []}
    resp_get = Api.put_emp((resp_add.json().get("data").get("id")), token_data, put_emp_data)
    print("修改后的状态", resp_get.json())

    respes_delete1 = Api.delete_emp(200, token_data)
    print("id不存在：",respes_delete1.json())

    respes_delete2 = Api.delete_emp(None, token_data)
    print("Id为空：",respes_delete2.json())

    respes_delete = Api.delete_emp((resp_add.json().get("data").get("id")), token_data)
    print("删除返回的结果：", respes_delete.json())
